using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication1.Data;
using WebApplication1.Models;
using Microsoft.AspNetCore.Authorization;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 图书管理控制器
    /// 提供图书的增删改查等RESTful API接口
    /// </summary>
    [ApiController] // 标记这是一个API控制器
    [Route("api/[controller]")] // 设置路由前缀为 api/Book
    public class BookController : ControllerBase
    {
        private readonly BookDbContext _context;

        public BookController(BookDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有图书列表
        /// GET: api/Book
        /// </summary>
        /// <returns>返回图书列表</returns>
        [HttpGet]
        public async Task<Result<List<Book>>> GetBooks()
        {
            try
            {
                var books = await _context.Books.ToListAsync();
                return Result<List<Book>>.Ok(books, "获取图书列表成功");
            }
            catch (Exception ex)
            {
                return Result<List<Book>>.Error($"获取图书列表失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 根据ID获取单本图书
        /// GET: api/Book/5
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>返回指定ID的图书</returns>
        [HttpGet("{id}")]
        public async Task<Result<Book>> GetBook(int id)
        {
            try
            {
                var book = await _context.Books.FindAsync(id);
                if (book == null)
                {
                    return Result<Book>.Error("未找到指定图书");
                }
                return Result<Book>.Ok(book, "获取图书成功");
            }
            catch (Exception ex)
            {
                return Result<Book>.Error($"获取图书失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 创建新图书
        /// POST: api/Book
        /// </summary>
        /// <param name="book">图书实体</param>
        /// <returns>返回新创建的图书</returns>
        [HttpPost]
        [Authorize]
        public async Task<Result<Book>> CreateBook(Book book)
        {
            try
            {
                _context.Books.Add(book);
                await _context.SaveChangesAsync();
                return Result<Book>.Ok(book, "图书创建成功");
            }
            catch (Exception ex)
            {
                return Result<Book>.Error($"图书创建失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 更新指定ID的图书
        /// PUT: api/Book/5
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <param name="book">更新后的图书实体</param>
        /// <returns>返回更新后的图书</returns>
        [HttpPut("{id}")]
        [Authorize]
        public async Task<Result<Book>> UpdateBook(int id, Book book)
        {
            try
            {
                if (id != book.Id)
                {
                    return Result<Book>.Error("请求的ID与图书ID不匹配");
                }

                _context.Entry(book).State = EntityState.Modified;
                await _context.SaveChangesAsync();
                return Result<Book>.Ok(book, "图书更新成功");
            }
            catch (Exception ex)
            {
                return Result<Book>.Error($"图书更新失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 删除指定ID的图书
        /// DELETE: api/Book/5
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>返回操作结果</returns>
        [HttpDelete("{id}")]
        [Authorize]
        public async Task<Result<bool>> DeleteBook(int id)
        {
            try
            {
                var book = await _context.Books.FindAsync(id);
                if (book == null)
                {
                    return Result<bool>.Error("未找到指定图书");
                }

                _context.Books.Remove(book);
                await _context.SaveChangesAsync();
                return Result<bool>.Ok(true, "图书删除成功");
            }
            catch (Exception ex)
            {
                return Result<bool>.Error($"图书删除失败：{ex.Message}");
            }
        }
    }
}